import 'dart:convert';

import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:taonile/config/config.dart';
import 'package:taonile/entity/bank_list_entity.dart';
import 'package:taonile/utils/singleton_manager.dart';
import 'package:taonile/utils/string_utils.dart';

import '../entity/receipt_address_entity.dart';
import '../http/api.dart';
import '../routers/routers.dart';
import '../views/default_views.dart';
import '../views/dialog_manager.dart';

///
/// 银行卡管理
///
class BankcardManager extends StatefulWidget {
  const BankcardManager({Key? key}) : super(key: key);

  @override
  State<BankcardManager> createState() => _BankcardManagerState();
}

class _BankcardManagerState extends State<BankcardManager> {
  List<BankListDataData> datas = [];
  var page = 1;

  @override
  void initState() {
    super.initState();
    getdatas();
  }

  getdatas() async {
    dynamic result = await Api.bankCardList({'page': page, 'limit': '20'});
    BankListEntity entity = BankListEntity.fromJson(result);
    setState(() {
      datas.clear();
      datas.addAll(entity.data.data);
    });
  }

  deleteDatas(String id) async {
    dynamic result = await Api.deleteBankCard({'id': id});
    var jsonResult = jsonDecode(jsonEncode(result));
    if (jsonResult['code'] == 1) {
      EasyLoading.showToast("删除成功");
      getdatas();
    }
  }

  Widget getListItem(int index) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 10.w, horizontal: 25.w),
      decoration: BoxDecoration(
        color: (Config.colorWhite),
        borderRadius: BorderRadius.circular(10.w),
      ),
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 20.w, horizontal: 25.w),
      child: Column(
        children: [
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Text(
                datas[index].bankName,
                style: TextStyle(fontSize: 28.sp, color: Config.colorText1),
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.w),
          ),
          Divider(
            color: Config.colorDivider,
            height: 2.w,
            thickness: 2.w,
          ),
          Padding(
            padding: EdgeInsets.only(top: 15.w),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '持卡人',
                    style: TextStyle(fontSize: 22.sp, color: Config.colorText3),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 3.w),
                  ),
                  Text(
                    datas[index].realName.isNotEmpty ? StringUtils.getNameHide(datas[index].realName) : '.',
                    style: TextStyle(fontSize: 26.sp, color: Config.colorText1),
                  ),
                ],
              ),
              Column(
                crossAxisAlignment: CrossAxisAlignment.end,
                children: [
                  Text(
                    '银行预留号码',
                    style: TextStyle(fontSize: 22.sp, color: Config.colorText3),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 3.w),
                  ),
                  Text(
                    StringUtils.getMiddleHideForMobile(datas[index].mobile),
                    style: TextStyle(fontSize: 26.sp, color: Config.colorText1),
                  ),
                ],
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 20.w),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    '银行卡号',
                    style: TextStyle(fontSize: 22.sp, color: Config.colorText3),
                  ),
                  Padding(
                    padding: EdgeInsets.only(top: 3.w),
                  ),
                  Text(
                    StringUtils.getMiddleHideForBankcard(datas[index].bankNumber),
                    style: TextStyle(fontSize: 26.sp, color: Config.colorText1, letterSpacing: 4.w),
                  ),
                ],
              ),
            ],
          ),
          Padding(
            padding: EdgeInsets.only(top: 15.w),
          ),
          Divider(
            color: Config.colorDivider,
            height: 2.w,
            thickness: 2.w,
          ),
          Padding(
            padding: EdgeInsets.only(top: 15.w),
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.end,
            children: [
              // InkWell(
              //   onTap: () {
              //     SingletonManager.instance.currentBankData=datas[index];
              //     Routes.navigateTo(context, Routes.mineBankcardAdd,
              //         params: {'accountId': datas[index].id}).then((value) {
              //       getdatas();
              //     });
              //   },
              //   child: Row(
              //     children: [
              //       Icon(
              //         Icons.edit_location_outlined,
              //         color: Config.colorText1,
              //         size: 25.w,
              //       ),
              //       Padding(
              //         padding: EdgeInsets.only(left: 7.w),
              //       ),
              //       Text(
              //         "编辑",
              //         style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
              //       ),
              //     ],
              //   ),
              // ),
              // Padding(
              //   padding: EdgeInsets.only(left: 20.w),
              // ),
              InkWell(
                onTap: () {
                  DialogManager.showDefaultDialog(context, "确认删除银行卡", () {
                    deleteDatas(datas[index].id);
                  });
                },
                child: Row(
                  children: [
                    Icon(
                      Icons.delete_forever,
                      color: Config.colorText1,
                      size: 25.w,
                    ),
                    Padding(
                      padding: EdgeInsets.only(left: 7.w),
                    ),
                    Text(
                      "删除",
                      style: TextStyle(fontSize: 24.sp, color: Config.colorText1),
                    ),
                  ],
                ),
              ),
            ],
          ),
        ],
      ),
    );
  }

  Future<bool> _onWillPop() {
    Routes.router.pop(context, {'result': 1});
    return Future.value(false);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: _onWillPop,
        child: Scaffold(
          backgroundColor: Config.colorBg2,
          appBar: AppBar(
            title: const Text('银行卡管理'),
            centerTitle: true,
            leading: IconButton(
                onPressed: () {
                  Routes.router.pop(context, {'result': 1});
                },
                icon: const Icon(Icons.arrow_back)),
          ),
          body: SizedBox(
            width: double.infinity,
            height: double.infinity,
            child: Stack(
              children: [
                Container(
                  width: double.infinity,
                  height: double.infinity,
                  padding: EdgeInsets.only(bottom: 90.w),
                  // decoration: BoxDecoration(
                  //   color: (Config.colorWhite),
                  //   borderRadius: BorderRadius.circular(12.w),
                  // ),
                  child: datas.isNotEmpty
                      ? ListView.builder(
                          shrinkWrap: true,
                          physics: const BouncingScrollPhysics(),
                          itemCount: datas.length,
                          padding: EdgeInsets.symmetric(vertical: 10.w, horizontal: 0.w),
                          itemBuilder: (BuildContext context, int index) {
                            return getListItem(index);
                          },
                        )
                      : showNullData(),
                ),
                Positioned(
                  bottom: 0,
                  child: Container(
                    width: 750.w,
                    height: 100.w,
                    padding: EdgeInsets.symmetric(vertical: 8.w, horizontal: 12.w),
                    child: ElevatedButton(
                      clipBehavior: Clip.antiAlias,
                      onPressed: () {
                        Routes.navigateTo(context, Routes.mineBankcardAdd).then((value) {
                          getdatas();
                        });
                      },
                      style: ElevatedButton.styleFrom(shape: const StadiumBorder()),
                      child: const Text('添加新银行卡'),
                    ),
                  ),
                ),
              ],
            ),
          ),
        ));
  }
}
